function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

var index = GetQueryString('Index');
$('.select2').select2();
$('#datepicker').datepicker({
    format: 'yyyy-mm-dd'

})
dataInit();

function dataInit() {
    $.ajax({
        type: "get",
        url: "http://www.puer.zone:30000/ProductDetailsInfo",
        async: true,
        data: {
            productId: index
        },
        success: function (data) {
            DataView(data);
        }
    });
}

function DataView(data) {
    var inpText = $('.new_item').find('input');
    for (var i = 0; i < inpText.length; i++) {
        inpText.eq(i).val(data[0][i]);
    }

    $('#pro_type').val(data[0][6]).trigger('change');
    $('#group1').val(data[0][7][0]).trigger('change');
    $('#group2').val(data[0][7][1]).trigger('change');

    var box = $('#pro_act');
    box.empty();
    for (var i = 0; i < data[0][8].length; i++) {
        var text = `<option value='${data[0][8][i]}'>${data[0][8][i]}</option>`;
        box.append(text);
    }
    $('#pro_act').val(data[0][8]).trigger('change');

    for (var i = 0; i < data[0][9].length; i++) {
        var text = `<tr index="${data[0][9][i][3]}">
		      		    		<td>${data[0][9][i][0]}</td>
		      		    		<td>${data[0][9][i][1]}</td>
		      		   
		      		    		<td>
		      		    			<input class="size_input form-control" style="width: 50px;height: 30px" type="text"/>
		      		    		</td>
		      		    	</tr>`;
        $('.format_data').append(text);
    }

    for (var i = 0; i < data[1].length; i++) {
        $('.img').eq(i).html('<img src="' + data[1][i] + '" width="85px" height="100px" style="vertical-align: top;">');
    }

    $('.detail_pic').html('<img src="' + data[0][10] + '" width="85px" height="100px" style="vertical-align: top;">');
    $('.file_img1').html('<img src="' + data[0][11] + '" width="85px" height="100px" style="vertical-align: top;">');
}

$(".select2").prop("disabled", true);
// console.log($(".sel").select2('val'));
///提交
$('.submit_all').click(function () {
    var textOnoff = true;

    var rows = $('.format_data').eq(0).find('tr');
    var size = [];
    for (var r = 0; r < rows.length; r++) {
        var cell = rows[r].cells;
        size[r] = [];
        size[r][0] = rows[r].attributes[0].nodeValue;
        for (var c = 0, i = 1; c < cell.length - 1; c++, i++) {
            size[r][i] = cell[c].innerHTML;
        }
        // var num = cell[2].innerHTML;
        // if (isNaN(num)) {
        //     layer.msg('请填写数字');
        //     textOnoff = false;
        //     break;
        //
        // } else {
        //     size[r][2] = num;
        // }
    }
    var box = $(".size_input");
    for (var i = 0; i < box.length; i++) {
        size[i][3] = box[i].value;
    }

    if (textOnoff) {
        $.ajax({
            type: "get",
            url: "http://www.puer.zone:30000/ProductNumberAddition",
            async: true,
            data: {
                productId: index,
                num: JSON.stringify(size)
            },
            success: function (data) {
                if (data == 'success') {
                    layer.msg('新增成功', {time: 500}, function () {
                        window.location.href = "http://www.puer.zone:9000/product_list.html";
                    });
                } else {
                    layer.msg('新增失败')
                }
            }
        });
    }

})
